//-------------------------------------------------------------------------------
//	file p_mem.c
//	brief
//		Pretty print memory buffers (implementation file)
//		Displays byte offset header and line offset
//		Displays the buffer in hex and ASCII
//
//	$Id: p_mem.c 3 2014-04-19 07:10:51Z ac.verbeck@gmail.com $
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//	Copyright and Disclaimer Notice
//
//	The MIT License (MIT)
//	
//	Copyright (c) 2014 A.C. Verbeck
//	
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//	
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//	
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//
//------------------------------------------------------------------------------

#include <stdio.h>
#include <string.h>
#include <stdint.h>

#include "p_mem.h"

void p_mem(uint8_t* buff, uint32_t length)
{
	uint32_t row, col, i=0;														//	Declare row, col, and i.  Set i to 0

	printf("      00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
	for (row=0; (i+1)<length; row++) {											//	Iterate through 16 byte wide rows
		printf("%04x: ",row*16);
		for (col=0; col<16; col++) {											//	Print hex
			i = row*16+col;														//	Calculate current position
			if (i<length)														//	Print hex value if the current index is in range
				printf("%02x", buff[i]);
			else																//	Print a blank if the current index is past the end
				printf("  ");
			printf(" ");														//	Print a space to keep alignment
		}
		printf("| ");															//	Add a vertical separator between hex and ASCII representations

		for (col=0; col<16; col++) {											//	Print ASCII representation
			i = row*16+col;														//	Calculate the current index
			if (i<length) {														//	Print the value if it is in range
				if (buff[i]>0x20 && buff[i]<0x7F) 								//	Print the ASCII value if applicable
					printf("%c", buff[i]);
				else															//	Else print a dot
					printf(".");
			} else																//	Nothing else to print, so break out of this for loop
				break;
		}
		printf("\n");															//	Goto next line
	}
}

//
//	End: p_mem.c
//
